//
//  XUITableViewCell.m
//  Etion
//
//  Created by  user on 11-3-17.
//  Copyright 2011 GuangZhouXuanWu. All rights reserved.
//

#import "XUITableViewCell.h"

#import "XUITableView.h"

@interface XUITableViewCell()
{
    UIView *_separatorView;
    
    BOOL _hiddenFollowSeparateLine;
    
    void(^_layoutSubviews)(UIView *contentView);
}

@end

@implementation XUITableViewCell

@synthesize szTag;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
	self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

	return self;
}

- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    
	return self;
}

-(void)setHiddenFollowSeparateLine:(BOOL)hiddenFollowSeparateLine
{
    _hiddenFollowSeparateLine = hiddenFollowSeparateLine;
    
    if(hiddenFollowSeparateLine == YES && _separatorView != nil)
    {
        [_separatorView removeFromSuperview];
        ReleaseObj(_separatorView);
        [self removeObserver:self forKeyPath:@"frame"];
    }
}

- (void)didMoveToSuperview
{
    [super didMoveToSuperview];
    
    XUITableView *tableView = nil;
    
    if([self.superview.superview isKindOfClass:[XUITableView class]])
        tableView = (XUITableView *)self.superview.superview;
    else if([UIDevice isIOS11OrNewer] && [self.superview isKindOfClass:[XUITableView class]])
        tableView = (XUITableView *)self.superview;
    
    if (tableView !=nil)
    {
        if(_hiddenFollowSeparateLine == NO && tableView.separatorExistFollowCell == YES)
        {
            if(_separatorView == nil)
            {
                _separatorView = [[UIView alloc]initWithFrame:CGRectMake(0, self.height - 0.5, self.width, 0.5)];
                [self insertSubview:_separatorView aboveSubview:self.contentView];
                
                [self addObserver:self forKeyPath:@"frame" options:0 context:NULL];
                
                _separatorView.backgroundColor = [UIColor lightGrayColor];
                if(tableView.separatorColor != nil)
                    _separatorView.backgroundColor = tableView.separatorColor;
                if([tableView respondsToSelector:@selector(separatorInset)])
                {
                    
                }
            }
        }
    }
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if(object == self && [keyPath isEqualToString:@"frame"])
    {
        if(_separatorView != nil)
        {
            [self bringSubviewToFront:_separatorView];
            
            CGRect frame = CGRectMake(0, self.height - 0.5, self.width, 0.5);
            XUITableView *tableView = (XUITableView *)self.superview.superview;
            if([tableView respondsToSelector:@selector(separatorInset)])
            {
                frame.origin.x += tableView.separatorInset.left;
                frame.size.width -= (tableView.separatorInset.left + tableView.separatorInset.right);
            }
            if([self respondsToSelector:@selector(separatorInset)])
            {
                frame.origin.x += self.separatorInset.left;
                frame.size.width -= (self.separatorInset.left + self.separatorInset.right);
            }
            _separatorView.frame = frame;
        }
    }
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    if(_layoutSubviews != NULL)
        _layoutSubviews(self.contentView);
}

-(void)layoutSubViewsBlock:(void (^)(UIView *))block
{
    ReleaseObj(_layoutSubviews);
    if(block != NULL)
        _layoutSubviews = [block copy];
}

- (void)dealloc
{
    ReleaseObj(_layoutSubviews);
    if(_separatorView != nil)
    {
        [self removeObserver:self forKeyPath:@"frame"];
        [_separatorView release_mm];
    }
    self.szTag=nil;
    superdealloc_mm;
}

@end

@implementation XUITableViewCell (Menu)

-(NSArray*)menuItem
{
    return nil;
}

-(CGRect)menuFrame
{
    return CGRectZero;
}

@end

@implementation XUITableViewCell (ActivityIndicatorView)

#define ActivityIndicatorViewTag 10347452

-(UIActivityIndicatorView*)createActivityIndicatorView
{
    UIActivityIndicatorView *indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    indicatorView.tag = ActivityIndicatorViewTag;
    indicatorView.hidesWhenStopped = YES;
    return [indicatorView autorelease_mm];
}

-(void)addActivityIndicatorViewInParentView:(UIView*)view
{
    UIActivityIndicatorView *indicatorView = [self createActivityIndicatorView];
    [view addSubview:indicatorView];
    
    indicatorView.center=CGPointMake(view.width/2, view.height/2);
    
    [indicatorView setTranslatesAutoresizingMaskIntoConstraints:NO];
    
    NSLayoutConstraint *l1=[NSLayoutConstraint constraintWithItem:indicatorView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
    NSLayoutConstraint *l2=[NSLayoutConstraint constraintWithItem:indicatorView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
    [view addConstraints:@[l1,l2]];
}

-(void)startActivityIndicator
{
    [(UIActivityIndicatorView *)[self viewWithTag:ActivityIndicatorViewTag] startAnimating];
}

-(void)stopActivityIndicator
{
    [(UIActivityIndicatorView *)[self viewWithTag:ActivityIndicatorViewTag] stopAnimating];
}

@end
